package com.meicet.adapter.utils

import android.os.Parcelable
import com.tencent.mmkv.MMKV

object SaveValue {

    val mmkv by lazy { MMKV.defaultMMKV()!! }

    fun saveString(key: String, value: String?) {
        mmkv.encode(key, value)
    }

    fun haveKey(key: String): Boolean {
        return mmkv.containsKey(key)
    }

    fun saveBoolean(key: String, value: Boolean) = mmkv.encode(key, value)
    fun getBoolean(key: String, defaultValue: Boolean = false): Boolean =
        mmkv.getBoolean(key, defaultValue)

    fun getString(key: String): String? = mmkv.decodeString(key)
    fun getString(key: String, default: String): String = mmkv.decodeString(key, default) ?: default

    fun getInt(key: String) = mmkv.decodeInt(key)
    fun getInt(key: String, default: Int) = mmkv.decodeInt(key, default)
    fun getLong(key: String, default: Long = 0) = mmkv.decodeLong(key, default)

    fun saveInt(key: String, value: Int) = mmkv.encode(key, value)
    fun saveLong(key: String, value: Long) = mmkv.encode(key, value)
    fun saveByteArray(key: String, value: ByteArray) = mmkv.encode(key, value)
    fun findByteArray(key: String) = mmkv.decodeBytes(key)

    inline fun <reified T : Parcelable> saveParcelable(key: String, obj: T?) = mmkv.encode(key, obj)

    inline fun <reified T : Parcelable> getParcelable(key: String, default: T): T? {
        return mmkv.decodeParcelable(key, T::class.java, default)
    }

    inline fun <reified T : Parcelable> getParcelable(key: String): T? {
        return mmkv.decodeParcelable(key, T::class.java)
    }

    fun remove(key: String) {
        mmkv.removeValueForKey(key)
    }

    fun delete(key: String) {
        mmkv.remove(key)
    }
}
